const { update, add, getCount, query, remove } = require('../../utils/dbTools');
const tableName = 'comment';
const patientTableName = 'patient';
const staffTableName = 'staff';

/**
 * 获取数据总条数
 * 
*/

const commentTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { doctorid, patientid } = req.query;
    if (!doctorid && !patientid) {
        output.code = 500;
        output.msg = "参数错误:缺少doctorid或者patientid, 必须传递其中之一";
        return res.send(output);
    }
    // 查询条件
    var map = {}
    // 如果传递了doctorid
    if (doctorid) {
        map.doctorid = doctorid;
    } else {
        map.patientid = patientid;
    }

    const result = await getCount(tableName, map);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
    }
    output.data = result;
    res.send(output);
}

/**
 *  根据医生id||患者id获取评论列表
 * 
*/
const commentList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { doctorid, patientid, page, size } = req.query;
    if (!doctorid && !patientid) {
        output.code = 500;
        output.msg = "参数错误:缺少doctorid和patientid, 必须提供其中之一";
        return res.send(output);
    }
    let sql = `select c.id,c.patientid,c.doctorid,c.status,c.content,c.rate,c.create_time,s.name as patient`
    sql += ` from ${tableName} c inner join ${staffTableName} s on c.patientid=s.id`;
    sql += ` where c.status=1`;
    if (doctorid) {
        sql += ` and c.doctorid=${doctorid}`;
    } else {
        sql += ` and c.patientid=${patientid}`;
    }
    sql += ` order by c.id desc`;
    // 如果传递了分页参数, 则实现分页查询
    if (page && size) {
        sql += `  limit ${(page - 1) * size},${size}`;
    }
    const result = await query(sql);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}

/**
 * 新增评论信息
 * 
*/

const commentAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { patientid, doctorid, rate, content } = req.body;
    if (!patientid || !doctorid || !content || !rate) {
        output.code = 500;
        output.msg = "参数错误: 缺少patientid,doctorid,rate,content";
        return res.send(output);
    }
    const data = {
        patientid, doctorid, content, rate, create_time: Date.now()
    }
    const result = await add(data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
        return res.send(output);
    }
    res.send(output);
}

/**
 *  删除评论信息
 * 
*/

const commentRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    // const result = await update(id, { status: -1 }, tableName);
    const result = await remove(id, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}

module.exports = {
    commentAdd,
    commentRemove,
    commentList,
    commentTotal
}